function createnewaccount() {    
    if (!$('#form-new').valid())
        return;
    $.ajax({
        url: '/index/createaccount/',
        dataType: 'json',
        type: 'post',
        async: false,
        data: {
            'username' :$("#username").val(),
            'nombre': $('#nombre').val(),
            'apellido': $('#apellido').val(),
            'email': $('#email').val(),
            'password': $('#password').val()
        },
        success: function(data) {
            if(data.response == 'error') {                
                alertdiv("#div-alert", '<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button> <span> <strong>' + data.message + '</strong></span></div>');
            } else {
                window.location.href = '/';
            }
        },
        error: function(a, b, c) {            
            alertdiv("#div-alert", '<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button> <span> <strong>Ocurrio un error inesperado</strong></span></div>');
        }
    });
}

$(document).ready(function() {
    jQuery.validator.setDefaults({
        debug: true,
        success: "valid"
    });
    $('#form-new').validate({
        rules: {
            "username": {
                required: true                
            },
            "nombre": {
                required: true                
            },
            "apellido": {
                required: true                
            },
            "email": {
                required: true,
                email: true
            },
            "password": {
                required: true,
                minlength: 5
            }, 
            "password_confirm": {
                required: true,
                minlength: 5,
                equalTo: "#password"
            }            
        }
    });
    $("#btncrear").click(createnewaccount);  
    $("#password_confirm").keyup(function(event){
        if(event.keyCode == 13){            
            createnewaccount();
        }
    });
});